package com.sx.zongzhi.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.sx.zongzhi.BaseActivity;
import com.sx.zongzhi.R;
import com.sx.zongzhi.adapter.MyFragmentPagerAdapter;
import com.sx.zongzhi.bean.ContactBean;
import com.sx.zongzhi.fragment.InsideContactFragment;
import com.sx.zongzhi.fragment.LevelFragment;
import com.sx.zongzhi.fragment.MyGroupFragment;
import com.sx.zongzhi.utils.BaseUtil;
import com.sx.zongzhi.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 选择收件人
 */
public class SelectPersonActivity extends BaseActivity {

    @BindView(R.id.tl_TabLayout)
    TabLayout tl_TabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    private List<Fragment> fragments=null;

    public List<ContactBean> contactBeanList = null;
    public  List<ContactBean> contactGroupList = null;
    public  List<ContactBean> contactOrgList = null;

    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_household_detail);
        setTitleName("收件人选择");
        setTitleRight("确定");
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        List<String> titles=new ArrayList<>();
        titles.add("站内联系人");
        titles.add("我的群组");
        titles.add("层级");
        ContactBean contactBean = (ContactBean) getIntent().getSerializableExtra("contactBean");

        if (contactBean != null){
            contactBeanList = contactBean.contactBeanList;
            contactGroupList = contactBean.contactGroupsBeanList;
            contactOrgList = contactBean.contactOrgBeanList;
        }
        fragments = new ArrayList<>();
        fragments.add(new InsideContactFragment());
        fragments.add(new MyGroupFragment());
        fragments.add(new LevelFragment());
        tl_TabLayout.setTabMode(TabLayout.MODE_FIXED);
        //为TabLayout添加tab名称
        tl_TabLayout.addTab(tl_TabLayout.newTab().setText(titles.get(0)));
        tl_TabLayout.addTab(tl_TabLayout.newTab().setText(titles.get(1)));
        MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), fragments, titles);
        viewPager.setAdapter(pagerAdapter);
        viewPager.setOffscreenPageLimit(2);
        //tabLayout加载viewpager
        tl_TabLayout.setupWithViewPager(viewPager);
    }

    @OnClick({R.id.tv_right})
    public void OnClick(View view){
        switch (view.getId()){
            case R.id.tv_right://确定
                List<ContactBean> selectContactList = new ArrayList<>();
                List<ContactBean> contactList = ((InsideContactFragment)fragments.get(0)).contactList;
                String userIds = "";
                if (contactList.size() > 0 ){
                    for (ContactBean contactBean : contactList){
                        if (contactBean.isSelect()){
                            userIds += contactBean.id + ",";
                            selectContactList.add(contactBean);
                        }
                    }
                }

                List<ContactBean> selectGroupList = new ArrayList<>();
                List<ContactBean> contactGroupList = ((MyGroupFragment)fragments.get(1)).myGroupList;
                String groupIds = "";
                if (contactGroupList.size() > 0){
                    for (ContactBean contactBean : contactGroupList){
                        if (contactBean.isSelect()){
                            groupIds += contactBean.id + ",";
                            selectGroupList.add(contactBean);
                        }
                    }
                }

                List<ContactBean> selectOrgList = new ArrayList<>();
                List<ContactBean> contactOrgList = ((LevelFragment)fragments.get(2)).levelBeenList;
                String orgIds = "";
                if (contactOrgList.size() > 0){
                    for (ContactBean contactBean : contactOrgList){
                        if (contactBean.rows != null && contactBean.rows.size() > 0){
                            for (ContactBean contactBean1 : contactBean.rows){
                                if (contactBean1.isSelect()){
                                    orgIds += contactBean1.id + ",";
                                    selectOrgList.add(contactBean1);
                                }
                            }
                        }
                    }
                }
                if (BaseUtil.isEmpty(userIds) && BaseUtil.isEmpty(groupIds) && BaseUtil.isEmpty(orgIds)){
                    ToastUtils.showToast(SelectPersonActivity.this,"选择接收人");
                    return;
                }
                Intent intent = new Intent();
                ContactBean contactBean = new ContactBean();
                contactBean.contactBeanList = selectContactList;
                contactBean.contactGroupsBeanList = selectGroupList;
                contactBean.contactOrgBeanList = selectOrgList;
                intent.putExtra("contactBean",contactBean);
                setResult(RESULT_OK,intent);
                finish();
                break;
        }
    }

}
